﻿/** 
 * Copyright 2010 NUMKIT.COM (contact: opensource@numkit.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 * 
 * @author 陈超
 * @version 1.0
 */
import flash.external.ExternalInterface;
import mx.utils.Delegate;
class Polyline {
	private var id:String;
	private var color:Number;
	private var lineWidth:Number;
	private var aPoint:Array;
	private var mc:MovieClip;
	private var config:Config;
	public function Polyline(id:String, color:Number, lineWidth:Number, aPoint:Array) {
		this.id = id;
		this.color = color;
		this.lineWidth = lineWidth;
		this.aPoint = aPoint;
	}
	public function draw(mc:MovieClip, config:Config):Void {
		this.mc = mc;
		this.config = config;
		//data transform
		var aPointL:Array = new Array();
		for (var i:Number = 0; i<aPoint.length; i++) {
			var p:Point = aPoint[i];
			var newX:Number = (p.x-config.xFrom)*(Stage.width-config.leftSpace-36)/(config.xTo-config.xFrom)+config.leftSpace+20;
			var newY:Number = Stage.height-20-(p.y-config.yFrom)*(Stage.height-36)/(config.yTo-config.yFrom);
			if (newX == Infinity || newY == Infinity) {
				continue;
			}
			aPointL.push(new Point(newX, newY));
		}
		//draw
		mc.lineStyle(lineWidth, color, 100, true, "normal", "square", "miter", 0);
		mc.moveTo(aPointL[0].x, aPointL[0].y);
		for (var i:Number = 0; i<aPointL.length-1; i++) {
			mc.lineTo(aPointL[i+1].x, aPointL[i+1].y);
			//trace(aPointL[i+1].x+":"+aPointL[i+1].y);
		}
		//event
		mc.onRollOver = Delegate.create(this, onRollOver);
		mc.onMouseMove = Delegate.create(this, onMouseMove);
		mc.onRollOut = Delegate.create(this, onRollOut);
	}
	public function onRollOver():Void {
		//var x:Number=DrawPanel.rootMC._xmouse;
		var y:Number = DrawPanel.rootMC._ymouse;
		//var rx:Number=(x-config.leftSpace-20)*(config.xTo-config.xFrom)/(Stage.width-config.leftSpace-36)+config.xFrom;
		var ry:Number = (Stage.height-20-y)*(config.yTo-config.yFrom)/(Stage.height-36)+config.yFrom;
		var text:String = String(formatNumber(ry));
		var fontSize:Object = Config.formatter.getTextExtent(text);
		DrawPanel.tipTF._width = fontSize.textFieldWidth;
		DrawPanel.tipTF._x = DrawPanel.rootMC._xmouse-fontSize.textFieldWidth;
		DrawPanel.tipTF._y = DrawPanel.rootMC._ymouse-14;
		DrawPanel.tipTF.text = text;
		DrawPanel.tipTF.setTextFormat(Config.formatter);
		DrawPanel.tipTF._visible = true;
		DrawPanel.tipTF.updateAfterEvent();
	}
	public function onMouseMove():Void {
		if (DrawPanel.tipTF._visible) {
			onRollOver();
		}
	}
	public function onRollOut():Void {
		DrawPanel.tipTF.text = "";
		DrawPanel.tipTF._visible = false;
	}
	//绝对值大于1的保留两位小数,小于等于1的原样输出
	public static function formatNumber(n:Number):Number {
		if (Math.abs(n)<=1) {
			return n;
		} else {
			return Math.round(n*100)/100;
		}
	}
}
